#include "pn532_rpi.h"
#include "endecrypt.h"

#include <stdint.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>

#define N 16



void *mythread1(void)
{
    //char arg[500] = "aa start -d 1 -a ohos.samples.jshelloworld.default -b ohos.samples.jshelloworld";
    char arg[500] = "aa start -d 1 -a com.huawei.ohos_car_controller.default -b ohos.samples.jshelloworld";
    system(arg);
    return NULL;
}


int main(int argc, char **argv)
{
    uint8_t buff[255];
    uint8_t uid[MIFARE_UID_MAX_LENGTH];
    //uint32_t pn532_error = PN532_ERROR_NONE;
    int32_t uid_len = 0;
    //uint8_t flag = 0;
    printf("主程序开始!\r\n");
    PN532 pn532;
    PN532_I2C_Init(&pn532);
    
    if (PN532_GetFirmwareVersion(&pn532, buff) == PN532_STATUS_OK)
    {
        printf("Found PN532 with firmware version: %d.%d\r\n", buff[1], buff[2]);
    }
    else
    {
        return -1;
    }
    PN532_SamConfiguration(&pn532);
    printf("Waiting for RFID/NFC card...\r\n");
    while (1)
    {
        while (1)
        {
            // Check if a card is available to read
            uid_len = PN532_ReadPassiveTarget(&pn532, uid, PN532_MIFARE_ISO14443A, 1000);
            if (uid_len == PN532_STATUS_ERROR)
            {
                printf(".");
            }
            else
            {
                printf("Found card with UID: ");
                for (uint8_t i = 0; i < uid_len; i++)
                {
                    printf("%02x ", uid[i]);
                }
                printf("\r\n");
                break;
            }
        }
        printf("开始调起应用\r\n");
        pthread_t id1;
        int ret = pthread_create(&id1, NULL, (void *)mythread1, NULL);
        if (ret)
        {
            printf("创建线程失败\r\n");
           
        } 
        sleep(4);          
     }      
}
